﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Panuon_FileDelete.Resources.Converters
{
    public class FileSizeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            decimal val = (decimal)value;

            if (val == 0)
            {
                return 0;
            }

            int byteConversion = 1024;

            IList<string> units = new List<string> { "B", "KB", "MB", "GB", "TB", "PB" };

            int i = 0;
            while (val >= byteConversion && i < units.Count - 1)
            {
                val /= byteConversion;
                i++;
            }

            return $"{val:N2} {units[i]}";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
